<?php

namespace App\Command\Make;

use Hyperf\Command\Annotation\Command;
use Hyperf\Utils\Str;
use Symfony\Component\Console\Input\InputOption;

#[Command]
class MakeModelCommand extends GeneratorCommand
{
    public function __construct()
    {
        parent::__construct('make:model');
        $this->setDescription('Create a new model class');
    }

    protected function getStub(): string
    {
        return __DIR__ . '/stubs/model.stub';
    }

    protected function getDefaultNamespace(): string
    {
        return $this->rootNamespace() . 'Model';
    }

    protected function buildClass($name): string
    {
        if (!($classVar = $this->input->getOption('table'))) {
            $classVar = Str::snake(lcfirst(str_replace($this->getNamespace($name) . '\\', '', $name)));
        }

        $replace = [
            '{{classVariable}}' => $classVar,
            '{{ classVariable }}' => $classVar,
        ];

        $serviceNamespace = $this->getNamespace($name);
        $replace["use {$serviceNamespace}\Model;\n"] = '';

        return str_replace(
            array_keys($replace), array_values($replace), parent::buildClass($name)
        );
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions(): array
    {
        return array_merge(parent::getOptions(), [
            ['table', 't', InputOption::VALUE_NONE, 'Generate a model for the given table..'],
        ]);
    }
}